﻿using System;

namespace Dsl.Ped.Common.Helpers
{
    /// <summary>
    /// Helper of the <see cref="object"/>
    /// </summary>
    public class ObjectHelper
    {
        /// <summary>
        /// Try to convert object to an object of a specific type, return
        /// default value of the type if the conversion is not successfull, or the <see cref="obj"/> is null
        /// </summary>
        /// <typeparam name="T">Type of the result value</typeparam>
        /// <param name="obj">Object to convert</param>
        /// <returns>Object converted to a specific type, if the conversion exists, default type value otherwise</returns>
        public static T TryConvert<T>(object obj)
        {
            try
            {
                var result = (T)obj;
                return result;
            }
            catch (Exception)
            {
                return default(T);
            }
        }
    }
}
